package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.data.type.VersionType;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The type collectionStructure specifies the overall content model of the document types that are collections of other
 * documents.
 * 
 * <pre>
 *  <xsd:complexType name="collectionStructure">
 * 		<xsd:sequence>
 * 			<xsd:element ref="meta"/>
 * 			<xsd:element ref="coverPage" minOccurs="0" maxOccurs="1"/>
 * 			<xsd:element ref="preface" minOccurs="0" maxOccurs="1"/>
 * 			<xsd:element ref="collectionBody"/>
 * 			<xsd:element ref="conclusions" minOccurs="0" maxOccurs="1"/>
 * 			<xsd:element ref="attachments" minOccurs="0" maxOccurs="1"/>
 * 		</xsd:sequence>
 * 		<xsd:attribute name="contains" type="versionType" default="originalVersion"/>
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public abstract class CollectionStructure implements Externalizable {

	private VersionType _versionType = VersionType.originalVersion;

	// Mandatory
	private final Meta _meta = new Meta();

	// Optional
	private CoverPage _coverPage;

	// Optional
	private Preface _preface;

	// Mandatory
	private final CollectionBody _collectionBody = new CollectionBody();

	public VersionType getVersionType() {
		return _versionType;
	}

	public void setVersionType(VersionType versionType) {
		_versionType = versionType;
	}

	public Meta getMeta() {
		return _meta;
	}

	public CoverPage getCoverPage() {
		return _coverPage;
	}

	public void setCoverPage(CoverPage coverPage) {
		_coverPage = coverPage;
	}

	public Preface getPreface() {
		return _preface;
	}

	public void setPreface(Preface preface) {
		_preface = preface;
	}

	public CollectionBody getCollectionBody() {
		return _collectionBody;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		_meta.writeExternal(out);

		if (_coverPage != null) {
			_coverPage.writeExternal(out);
		}
		if (_preface != null) {
			_preface.writeExternal(out);
		}

		_collectionBody.writeExternal(out);
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		in.readNextStartElement();

		if (Meta.ELEMENT == in.getLocalName()) {
			_meta.readExternal(in);
		}

		if (CoverPage.ELEMENT == in.getLocalName()) {
			_coverPage = new CoverPage();
			_coverPage.readExternal(in);
			in.readNextStartElement();
		}

		if (Preface.ELEMENT == in.getLocalName()) {
			_preface = new Preface();
			_preface.readExternal(in);
			in.readNextStartElement();
		}

		_collectionBody.readExternal(in);
	}

	protected final void internalAccept(AKVisitor visitor) {
		_meta.accept(visitor);
		
		if (_coverPage != null) {
			_coverPage.accept(visitor);
		}
		
		if (_preface != null) {
			_preface.accept(visitor);
		}
		
		_collectionBody.accept(visitor);
	}
	
}